package cfca.paperless.client.servlet._9_unite;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.servlet.PaperlessUniteClient;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * 联合接口(合成多媒体数据,自动化签章)
 * @author clsu
 *
 */
public class PaperlessUniteTest02 {

    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();// 开始时间
            
            PaperlessUniteClient clientBean = new PaperlessUniteClient(PaperlessConfig.uniteUrl, 10000, 60000);

            String filePath = "./TestData/file.pdf";
            byte[] pdfFileData = FileUtils.readFileToByteArray(new File(filePath));
            
            ////////////////////////////合成多媒体(synthesizeAutoMultiPdf)///////////////////////////////
            byte[] handwritingImage1 = FileUtils.readFileToByteArray(new File("./TestData/handwriting.png"));
            byte[] handwritingData1 = handwritingImage1;
            byte[] identificationFrontData = FileUtils.readFileToByteArray(new File("./TestData/1.png"));
            byte[] identificationBackData = FileUtils.readFileToByteArray(new File("./TestData/2.png"));
            List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
            list1.add(new MultiDataBean(handwritingData1, "handwritingData.dat", "2"));
            list1.add(new MultiDataBean(handwritingImage1, "handwritingImage.png", "2"));
            list1.add(new MultiDataBean(identificationFrontData, "identificationFrontImage.png", "2"));
            list1.add(new MultiDataBean(identificationBackData, "identificationBackImage.png", "2"));
            String proofHashAndMultiData[] = PaperlessUtil.getProofHashAndMultiData(list1);
            String multiData =  "[" + proofHashAndMultiData[1] + "]";
            clientBean.addSynthesizeAutoMultiPdfParams(multiData);
            
            /////////////////////////////////蓋章(sealAutoPdf)////////////////////////////////////////
            String sealCode = "org001";//0613sm201
            String sealPassword = "11111111";
            // 可以为空，不为空则此图片会替代此处印章的印模图片
            byte[] handwritingImage = FileUtils.readFileToByteArray(new File("./TestData/zhangfei.png"));
            SealUserInfo sealUserInfo = new SealUserInfo("张三", "beijing", "reason");
            // 3=关键字签章
            SignLocation signLocation = new SignLocation(1, 100, 100);//new SignLocation("人签名", "R", 0, 0);
            // 生成签章策略文件
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, "", sealUserInfo, signLocation);
            clientBean.addSealAutoPdfParams(sealStrategyXml);
            
            
            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;
            
            byte[] sealedPdfData = clientBean.uniteReqAndRes(pdfFileData, operatorCode, channelCode);

            String errorRsString = PaperlessUtil.isError(sealedPdfData);

            // 处理结果为正常，保存签章后的PDF文件到本地目录下
            if ("".equals(errorRsString)) {
                System.out.println("OK:sealAutoPdf");
                System.out.println(sealedPdfData.length);
                IoUtil.write("./TestData/output/sealedPdf-" + new File(filePath).getName() + ".pdf", sealedPdfData);
            } else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("NG:sealAutoPdf");
                System.out.println(sealedPdfData.length);
                System.out.println(new String(sealedPdfData, "UTF-8"));
            }
            
            long timeEnd = System.currentTimeMillis();// 结束时间
            System.out.println("time:" + (timeEnd - timeStart));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
